import { packedData, RequestCode, UserAction, RoomAction, GameAction } from "../GlobalData/DataStruct";

export default class WebSocket extends Laya.Script {
   private socketObj:Laya.Socket;
    
    constructor() 
    { 
        super();
        //1.创建Laya.Socket对象
        this.socketObj=new Laya.Socket()
        //2.通过Url连接服务器
        this.socketObj.connectByUrl("ws://localhost:8888")
        //3.四种状态的监听
        this.socketObj.on(Laya.Event.OPEN, this, this.openHandler);
        this.socketObj.on(Laya.Event.MESSAGE, this, this.receiveHandler);
        this.socketObj.on(Laya.Event.CLOSE, this, this.closeHandler);
        this.socketObj.on(Laya.Event.ERROR, this, this.errorHandler);
    }

    /**打包数据，返回json字符串 */
    packData(requestCode:any,actionCode:any,data:any)
    {
        let packed_data=new packedData()
        packed_data.RequestCode=requestCode
        packed_data.ActionCode=actionCode
        packed_data.data=data
        //返回json字符串
        return JSON.stringify(packed_data)
    }
    /**发送消息 */
    sendMsg(msg:any)
    {
        this.socketObj.send(msg)
    }



    /**成功连接回调 */
    openHandler(){}
    /**收到信息回调 */
    receiveHandler(data:any)
    {
        // console.log("-----receive-----")
        // console.log(data)
        let json_data=JSON.parse(data)
        switch(json_data.RequestCode)
        {
            case RequestCode.USER:
                switch(json_data.ActionCode)
                {
                    case UserAction.LOGIN:
                        //将收到的消息分发到login_scene
                        Laya.stage.event("login",[json_data.data])
                        break;
                    case UserAction.SIGNUP:
                        Laya.stage.event("register",[json_data.data])
                        break;
                }
                break
            case RequestCode.ROOM:
                switch(json_data.ActionCode)
                {
                    case RoomAction.CREATE:
                        //将消息分发到create_callback函数
                        Laya.stage.event("create",[json_data])
                        break
                    case RoomAction.LIST:
                        //将消息分发到list_callback函数
                        Laya.stage.event("list",[json_data.data])
                        break
                    case RoomAction.QUIT:
                        //将消息派发到quit_callback函数
                        Laya.stage.event("quit",[json_data.data])
                        break
                    case RoomAction.JOIN:
                        //将消息派发到join_callback函数
                        Laya.stage.event("join",[json_data.data])
                        break
                    case RoomAction.READY:
                        //将消息派发到ready_callback函数
                        Laya.stage.event("ready",[json_data.data])
                        break
                    case RoomAction.START:
                        //将消息派发到start_callback函数
                        Laya.stage.event("start",[json_data.data])
                        break
                    case RoomAction.UNREADY:
                        //将消息派发到unready_callback函数
                        Laya.stage.event("unready",[json_data.data])
                        break
                }
                break
            case RequestCode.GAME:
                switch(json_data.ActionCode)
                {
                    case GameAction.MOVE:
                        //将消息转发到move_callback
                        Laya.stage.event("move",[json_data.data])
                        break
                    case GameAction.OVER:
                        //将消息转发到over_callback
                        Laya.stage.event("over",[json_data.data])
                        break
                }
                break
        }
    }
    /**关闭连接回调 */
    closeHandler(){}
    /**发生错误回调 */
    errorHandler(){}
    


    onDisable(): void {
    }
}